package com.jyhz.admin.utils;

import java.util.Random;

/**
 * @author: 谢君臣
 * @create: 2020-08-22 16:58
 * @founction: 随机验证吗
 */
public class RandomGen {
    public static String codeGen(){
//        char [] codeSequence={'A','B','C','D','E','F','G','H','I','J',
//                'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
//                '1','2','3','4','5','6','7','8','9'};
        char [] codeSequence={'1','2','3','4','5','6','7','8','9','0'};
        Random random =new Random();
        StringBuilder sb=new StringBuilder();//动态字符串，String创建的字符串不能修改
        int count=0;//计数器确定产生的是四位验证码
        while(true){
            //随机产生一个下标，通过下标取出字符数组对应的字符
            char c=codeSequence[random.nextInt(codeSequence.length)];
            //假设取出来的字符在动态字符串中不存在，代表没有重复
            if (sb.indexOf(c+"")==-1) {
                sb.append(c);//追加到动态字符串中
                count++;
                if (count==6) {
                    break;
                }
            }
        }
        return sb.toString();
    }

}
